﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.BLL;

/**************************************
*
* author:李朴
* company:上海沐雪网络科技有限公司
* qq:23002807
* website:http://uweixin.cn
* taobao:http://uweixin.cn/portalpage/weixin_news_detail.aspx?id=2102
* createDate:2013-11-1
* update:2014-12-30
* 
***********************************/
namespace MxWeiXinPF.Web.admin.toupiao
{
    public partial class member_info : Web.UI.ManagePage
    {
        //1e2124dd04e11d01b9df2865f85944be
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型

        wx_tp_members actBll = new wx_tp_members();
        protected int actId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = MXRequest.GetQueryString("action");
            int id = 0;
            actId = MyCommFun.RequestInt("actid");
            if (!string.IsNullOrEmpty(_action) && _action == MXEnums.ActionEnum.Edit.ToString())
            {

                this.action = MXEnums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out  id))
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!actBll.Exists(id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {

                BLL.wx_tp_action actBll = new BLL.wx_tp_action();
                Model.wx_tp_action actModel = actBll.GetModel(actId);
                this.litactName.Text = actModel.name;


                if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(id);
                }

            }
        }




        #region 赋值操作=================================
        //1e2124dd04e11d01b9df2865f85944be
        private void ShowInfo(int id)
        {
            hidid.Value = id.ToString();
            Model.wx_tp_members memberEntity = actBll.GetModel(id);

            txtname.Text = memberEntity.name;
            txttel.Text = memberEntity.tel;
            imgimgurl.ImageUrl = memberEntity.imgurl;
            txtimgurl.Text = memberEntity.imgurl;
            txtremark.Value = memberEntity.miaoshu;
            txtSortId.Text = memberEntity.sort_id.ToString();
            lblbianma.Text = memberEntity.bianma;
            lblbpiao.Text = memberEntity.piao.ToString();
            rblStatus.SelectedValue = memberEntity.status.ToString();
        }

        #endregion



        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();
            int id = MyCommFun.Str2Int(hidid.Value);
            #region  //先判断
            string strErr = "";

            if (this.txtname.Text.Trim().Length == 0)
            {
                strErr += "姓名不能为空！";
            }
            if (this.txtimgurl.Text.Trim().Length == 0  )
            {
                strErr += "图片不能为空！";
            }
            
            if (strErr != "")
            {
                JscriptMsg(strErr, "back");
                return;
            }
           
            #endregion

            #region 赋值
            Model.wx_tp_members memberModel = new Model.wx_tp_members();


            if (id > 0)
            {
                memberModel = actBll.GetModel(id);
            }

            memberModel.name = txtname.Text.Trim();
            memberModel.tel=txttel.Text.Trim();
            memberModel.imgurl = txtimgurl.Text.Trim();
            memberModel.miaoshu = txtremark.Value.Trim();
            memberModel.sort_id = MyCommFun.Obj2Int(txtSortId.Text);
            memberModel.status = MyCommFun.Obj2Int(rblStatus.SelectedItem.Value);
            #endregion

            if (id <= 0)
            {  //新增
                memberModel.actId = actId;
                memberModel.createdate = DateTime.Now;
                memberModel.status =1;
                memberModel.piao = 0;
                memberModel.fromOrgin = 2;
                memberModel.creator = weixin.id.ToString();
                
                id = actBll.Add(memberModel);


                AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加投票项，主键为" + id); //记录日志//1e2124dd04e11d01b9df2865f85944be
                JscriptMsg("添加投票活动成功！", "member_list.aspx?actid="+actId);
            }
            else
            {   //修改
                //1修改主表
                actBll.Update(memberModel);
                AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改投票项，主键为" + id); //记录日志
                JscriptMsg("修改投票活动成功！", "member_list.aspx?actid="+actId);
            }

        }

    }
}